<?php
/**
 * 显示一个input
 *
 */
function form_input($label, $name, $value, $required, $format='', $type='text', $readonly='', $span=3)
{
  if(is_string($value)){
    $val = $value;
  } else {
    $val = empty($value)?'':$value[$name];
  }
  $span2 = 12-$span;

  $labelHTML = '<label class="col-sm-'.$span.' control-label">'.$label.'</label>';

  $inputHTML = '<input '.$readonly.' type="'.$type.'" name="'.$name.'" class="form-control" data-format="'.$format.'" data-required="'.$required.'"  placeholder="请输入'.str_replace(' ', '', $label).'" value="'.$val.'">';
  if($label==''){
    return '<div class="form-group">'.$inputHTML.'</div>';
  } else {
    return '<div class="form-group">'.$labelHTML.'<div class="col-sm-'.$span2.'">'.$inputHTML.'</div></div>';
  }
}

/**
 * 显示一个select
 * @valueParam $data中对应的value字段
 * @optionParam $data中对应的option显示字段
 */
function form_select($label, $name, $value, $data, $required=true, $valueParam='id', $optionParam='name', $enable=true)
{


  if(is_string($value)){
    $val = $value;
  } else {
    $val = empty($value)?'':$value[$name];
  }


  $disabled = $enable?'':'disabled';
  $labelHTML = '<label class="col-sm-3 control-label">'.$label.'</label>';
  $selectHTML = '<select type="text" name="'.$name.'" class="form-control" data-required="'.$required.'" '.$disabled.'>';
  $optionsHTML = '';

  if(is_string($data)){
    $data = json_decode($data, true);
  }
//  echo json_encode($data);
  foreach ($data as $item) {

    if($item[$valueParam] ==  $val){
      $selected = 'selected';
    } else {
      $selected = '';
    }
    $optionsHTML = $optionsHTML.'<option value="'.$item[$valueParam].'" '.$selected.'>'.$item[$optionParam].'</option>';
  }
  //echo $optionsHTML;

  if($label==''){
    return '<div class="form-group">'.$selectHTML.$optionsHTML.'</select></div>';

  } else {
    return '<div class="form-group">'.$labelHTML.'<div class="col-sm-9">'.$selectHTML.$optionsHTML.'</select></div></div>';

  }
}

/**
 * 显示textarea
 */
function form_textarea($label, $name, $value, $required){
  $val = empty($value)?'':$value[$name];
  $labelHTML = '<label class="col-sm-3 control-label">'.$label.'</label>';
  $textareaHTML = '<textarea rows=4 name="'.$name.'" class="form-control" data-required="'.$required.'"  placeholder="请输入'.str_replace(' ', '', $label).'" value="'.$val.'">'.$val.'</textarea>';
  if($label==''){
    return '<div class="form-group">'.$textareaHTML.'</div>';
  } else {
    return '<div class="form-group">'.$labelHTML.'<div class="col-sm-9">'.$textareaHTML.'</div></div>';
  }
}

/**
 * 显示日期选择
 */
 function form_datepicker($label, $name, $value, $required, $format=''){
   if(is_string($value)){
     $val = $value;

     if($value == '0000-00-00 00:00:00'){
       $val = '';
     }
     //$val = date("Y-m-d",strtotime($value));
   } else {
     $val = empty($value)?'':date("Y-m-d",strtotime($value[$name]));
   }

   $labelHTML = '<label class="col-sm-3 control-label">'.$label.'</label>';
   $inputHTML = '<input type="text" data-dom="datepicker" name="'.$name.'" class="form-control" data-format="'.$format.'" data-required="'.$required.'"  placeholder="请选择日期" value="'.$val.'">';
   if($label==''){
     return '<div class="form-group">'.$inputHTML.'</div>';
   } else {
     return '<div class="form-group">'.$labelHTML.'<div class="col-sm-9">'.$inputHTML.'</div></div>';
   }
 }

?>
